<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('规则')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">规则名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="stcrName" placeholder="" type="text">
                </div>
                <label class="layui-form-label">时间粒度</label>
                <div class="layui-input-inline">
                    <select dict-code="sys_notice_type" lay-required name="granulatrty">
                        <option value="">全部</option>
                        <option value="1">分</option>
                        <option value="2">时</option>
                        <option value="3">日</option>
                        <option value="4">周</option>
                        <option value="5">月</option>
                        <option value="6">年</option>
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="exchange-query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="futures-table" lay-filter="futures-table"></table>
    </div>
</div>
</body>

<script id="role-toolbar" type="text/html">
    <button sec:authorize="hasPermission('/stock/rules/add','stock:rules:add')"
            class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button sec:authorize="hasPermission('/stock/rules/remove','stock:rules:remove')" class="pear-btn pear-btn-md"
            lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script id="role-bar" type="text/html">
    <button sec:authorize="hasPermission('/stock/rules/edit','stock:rules:edit')"
            class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button sec:authorize="hasPermission('/stock/rules/remove','stock:rules:remove')"
            class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>


<script id="rules-granulatrty" type="text/html">
    {{#if (d.granulatrty === '1') { }}
    分
    {{# }else if(d.granulatrty === '2') { }}
    时
    {{# }else if(d.granulatrty === '3') { }}
    日
    {{# }else if(d.granulatrty === '4') { }}
    周
    {{# }else if(d.granulatrty === '5') { }}
    月
    {{# }else if(d.granulatrty === '6') { }}
    年
    {{# } }}
</script>

<script id="rules-status" type="text/html">
    {{#if (d.status === '1') { }}
    有效
    {{# }else if(d.status === '0') { }}
    无效
    {{# } }}
</script>

<script id="rules-type" type="text/html">
    {{#if (d.type === '1') { }}
    技术
    {{# }else if(d.type === '2') { }}
    基本面
    {{# }else if(d.type === '3') { }}
    财务
    {{# } }}
</script>

<script id="rules-suitType" type="text/html">
    {{#if (d.suitType === '1') { }}
    股票
    {{# }else if(d.suitType === '2') { }}
    基金
    {{# }else if(d.suitType === '3') { }}
    期货
    {{# }else if(d.suitType === '4') { }}
    可转债
    {{# } }}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let common = layui.common;

        let MODULE_PATH = "/stock/rules/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '规则编码', field: 'stcrCode', align: 'center', width: 110},
                {title: '规则名称', field: 'stcrName', align: 'center'},
                {title: '时间粒度', field: 'granulatrty',templet: '#rules-granulatrty', align: 'center'},
                {title: '规则状态', field: 'status',templet: '#rules-status', align: 'center'},
                {title: '类型', field: 'type',templet: '#rules-type', align: 'center'},
                {title: '适用类型', field: 'suitType',templet: '#rules-suitType', align: 'center'},
                {title: '规则描述', field: 'stcrDesc', align: 'center'},
                {title: '操作', toolbar: '#role-bar', align: 'center', width: 195}
            ]
        ]

        table.render({
            elem: '#futures-table',
            url: MODULE_PATH + 'list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(futures-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(futures-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(exchange-query)', function (data) {
            table.reload('futures-table', {where: data.field})
            return false;
        });

        form.on('switch(role-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "enable";
            } else {
                operate = "disable";
            }
            let loading = layer.load()
            $.ajax({
                url: '/stock/rules/' + operate,
                data: JSON.stringify({roleId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'id');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('role-table');
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            table.reload('role-table');
        }
    })
</script>
</html>